<?php
	include_once 'isLogin.php';

	$table_name = "companies_accounting";
	$key_index = array('title','content','upid','state');
	
	$colors = array(
				'state'=>array('0'=>'#ff0000','1'=>'#16d316'),
			);
	$names = array(
				'state'=>array('0'=>'禁用','1'=>'可用'),
			);
      
	//查询
	if($_GET['submit']='搜索'){
		$table="users";
		$name="$_GET[name]";
	        if($_GET[name]!='' && $_GET[name]!=null){
		$select_sql = " and c.title like '%".$name."%'";
		}
		
	   }
//
//	//添加
//	if($_GET['action']=='add'){
//		
//		//插入数据
//		if(is_array($_POST['values'])){
//			foreach($_POST['values'] as $k=>$v){
//				$arr[$key_index[$k]] = $v;
//			}
//		}
//		$commonc->insert($table_name,$arr);
//		$id = $commonc->insert_id();
//		
//		//排序
//		$arr = array('sort'=>$id);
//		$commonc->update($table_name, $arr, "id='".$id."'");
//		
//		//查询记录
//		$data = $commonc->getInfo($table_name, $id);
//		
//		if($id!=''){
//			$return='<tr line_id="'.$id.'">
//			<td> <input type="checkbox" name="checkbox" value="'.$id.'" /></td>
//			<td value_id="'.$id.'">'.$data['title'].'</td>
//			<td value_id="'.$id.'">'.$data['content'].'</td>
//			<td>&nbsp;</td>
//			<td class="xiugai">
//				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'up\',\''.$_basename.'?action=sort\')"><img src="images/sort-up.png"/></a>
//				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'down\','.$_basename.'?action=sort\')"><img src="images/sort-down.png"/></a>
//			</td>
//			<td class="xiugai"><a href="javascript:;" style="color: '.$colors['state'][$data['state']].'" onclick="state(\''.$id.'\',\'state\',\''.$_basename.'\',$(this))">'.$names['state'][$data['state']].'</a></td>
//			<td class="xiugai">
//			<a href="javascript:;" onclick="edit(\''.$id.'\',$(this),\''.$_basename.'?action=edit\')">修改</a>';
//			if($data['upid']==false) $return.='<a href="'.$_basename.'?id='.$id.'">子类</a>';
//			$return.='</td></tr>';
//			$arr=array('error'=>'0','content'=>$return);
//		}else{
//			$arr=array('error'=>'1','content'=>'添加失败');
//		}
//		echo json_encode($arr);
//		exit();
//	}
//	
//	//删除
//	if($_GET['action']=='del'){
//		$delFile = array(
//				array('key'=>'img','dir'=>'../upload/test/large/'),
//				array('key'=>'img','dir'=>'../upload/test/middle/'),
//				array('key'=>'img','dir'=>'../upload/test/small/'),
//				array('key'=>'file','dir'=>'../upload/test/file/'),
//		);
//		echo $commonc->del($table_name,$_POST['id'],$delFile);
//		exit();
//	}
//	
//	//修改
	if($_GET['action']=='edit'){
		$arr = array( 'state'=>2);
                if($_GET['id'])
                {
                  echo $commonc->update($table_name,$arr,"id='".$_GET['id']."'");  
                }
		
		exit();
	}
//	
//	//修改状态
//	if($_GET['action']=='state'){
//		echo $commonc->changeState($table_name,$_POST['id'],$_POST['key'],$colors[$_POST['key']],$names[$_POST['key']]);
//		exit();
//	}
//	
//	//排序
//	if($_GET['action']=='sort'){
//		if($_POST['type']=='down') $type = "prev";
//		if($_POST['type']=='up') $type = "next";
//		$sort = $commonc->sortChange($table_name, $_POST['id'], $type);
//		echo $sort['id'];
//		exit();
//	}

	/**
         * 获取公司名称 本月成功交易金额
         */
	$where = " where o.state = 1 ";
	$sql = $table_name." o  left join  companies c on o.company_id = c.id";
	$sql.= $where;
        $key = "DATE_FORMAT(o.date,'%Y-%m') as month,o.company_id,o.this_profit as traded_money,o.last_remainprofit as remain_money,o.sum_money,c.title,o.id";
	$row=$commonc->getList($sql,$sql,$key);
       
     
      /**
         * 获取本月交易中交易金额
         */
        if(is_array($row['result']))
        {
            foreach ($row['result'] as $k => $value) {
                 
                 $where = " where o.trading_status = 1 and  DATE_FORMAT(o.trade_success_date,'%Y-%m') <>'".$value['month']."' and DATE_FORMAT(o.order_date,'%Y-%m') = '".$value['month']."' and p.company_id='".$value['company_id']."' group by company_id";
	         $sql = "company_order  o  left join  company_product  p on o.product_id = p.id ";
	         $sql.= $where;
                 $key = "DATE_FORMAT(o.trade_success_date,'%Y-%m') as month,p.company_id,sum(pay_money) as trading_money";
                 $result = $commonc->getList($sql,$sql,$key);
                 $trading_money = $result['result'][0]['trading_money'];
               
                 $value['trading_money']=$trading_money;
                 $row['result'][$k] = $value;
                
            }
          
        }  
        
        $smarty->assign("select_name",$name);
	$smarty->assign("select",$row['result']);
	$smarty->assign('page',$row['page']);
	$smarty->display($_filename.".html");
	
?>